package com.party.mobile.biz.mall;

import com.google.common.collect.Maps;
import com.party.common.utils.LangUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.mall.MallBrand;
import com.party.core.model.mall.MallCategory;
import com.party.core.model.mall.MallCategorySiftSpec;
import com.party.core.service.mall.IMallBrandService;
import com.party.core.service.mall.IMallCategoryService;
import com.party.core.service.mall.IMallSpecService;
import com.party.mobile.web.dto.AjaxResult;
import com.party.mobile.web.dto.mall.output.MallCategoryOutput;
import com.party.mobile.web.dto.mall.output.MallCategorySiftOutput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description: 商城分类业务
 * @Author: yangshoukun
 * @Date: 2018/4/16 16:15
 */
@Service
public class MallCategoryBizService {

    @Autowired
    private IMallCategoryService mallCategoryService;

    @Autowired
    private IMallBrandService mallBrandService;

    @Autowired
    private IMallSpecService mallSpecService;

    /**
     * 获取商城一级分类列表
     * @return
     */
    public AjaxResult list() {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        List<MallCategory> list = mallCategoryService.selectCategory();

        List<MallCategoryOutput> outputs = LangUtils.transform(list, output -> {
           MallCategoryOutput out = new MallCategoryOutput();
           out.setId(output.getId());
           out.setName(output.getName());
           return  out;
        });
        ajaxResult.setData(outputs);
        return ajaxResult;
    }

    /**
     * 获取二级分类列表
     * @param id
     * @return
     */
    public AjaxResult getDetail(String id) {
        AjaxResult ajaxResult = new AjaxResult();

        if (StringUtils.isEmpty(id)) {
            ajaxResult.setSuccess(false);
            ajaxResult.setDescription("一级分类id不能为空");
            return ajaxResult;
        }

        List<MallCategory> list = mallCategoryService.getByParentId(id);

        List<MallCategoryOutput> outputs = LangUtils.transform(list, output -> {
            MallCategoryOutput out = new MallCategoryOutput();
            out.setId(output.getId());
            out.setName(output.getName());
            out.setPic(output.getPic());
            return  out;
        });

        ajaxResult.setSuccess(true);
        ajaxResult.setData(outputs);

        return ajaxResult;
    }

    /**
     * 分类筛选面板数据
     * @param id
     * @return
     */
    public AjaxResult siftData(String id) {
        AjaxResult ajaxResult = new AjaxResult();

        if (StringUtils.isEmpty(id)) {
            ajaxResult.setSuccess(false);
            ajaxResult.setDescription("二级分类id不能为空");
            return ajaxResult;
        }

        MallCategorySiftOutput mallCategorySiftOutput = new MallCategorySiftOutput();
        // 品牌
        List<MallBrand> brands = mallBrandService.findByCategoryId(id);
        // 规格
        List<MallCategorySiftSpec> specs = mallSpecService.findSpecWithValues(id);

        List<Map<String, String>> outBrands = LangUtils.transform(brands, brand -> {
           Map<String, String> map = Maps.newHashMap();
           map.put("id", brand.getId());
           map.put("name", brand.getName());
           return map;
        });

        mallCategorySiftOutput.setBrandList(outBrands);
        mallCategorySiftOutput.setSpecList(specs);

        ajaxResult.setData(mallCategorySiftOutput);

        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    /**
     * 根据id获取分类
     *
     * @param id 分类id
     * @return
     */
    public MallCategoryOutput findById(String id) {
        MallCategory mallCategory = mallCategoryService.get(id);
        MallCategoryOutput output = new MallCategoryOutput();
        output.setId(mallCategory.getId());
        output.setName(mallCategory.getName());
        output.setPic(mallCategory.getPic());
        return output;
    }
}
